﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MpSoft.Library.DAO.Contents;
using MpSoft.Library.DTO;
using MpSoft.Basic.Configuration;

namespace MpSoft.Library.BUSS.Contents
{

    #region  New
    public class NewsBUSS
    {
        private const string NEW_ALL_KEY = "Mp.new.all-{0}-{1}}";
        private const string NEW_BY_ID_KEY = "Mp.new.id-{0}";

        private static void RemoveCache()
        {
            MpCache.RemoveByPattern(NEW_ALL_KEY);
            MpCache.RemoveByPattern(NEW_BY_ID_KEY);
        }

        public static int News_Add(News obj)
        {
            var result = NewsDAO.News_Add(obj);
            if (result > 0)
            {
                RemoveCache();
            }
            return result;
        }
        public static int News_Update(News obj)
        {
            var result = NewsDAO.News_Update(obj);
            if (result > 0)
            {
                RemoveCache();
            }
            return result;
        }

        public static int News_Delete(int NewID)
        {
            var result = NewsDAO.News_Delete(NewID);
            if (result > 0)
            {
                RemoveCache();
            }
            return result;
        }

        public static News News_ByID(int NewsID)
        {
            string key = string.Format(NEW_BY_ID_KEY, NewsID);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (News)obj;
            var listNews = News_List(NewsID, -1, 0, 0, null, int.MaxValue - 1, false, false);
            News obj2=null;
            if (listNews != null)
                obj2 = listNews.FirstOrDefault();
            if (obj2 != null)
                MpCache.Max(key, obj2);
            return obj2;
        }

        public static List<News> News_List(int CategoryID, int IsActive, int SortOrder)
        {
            string key = string.Format(NEW_BY_ID_KEY, CategoryID, IsActive);
            object obj = MpCache.Get(key);
            if (obj != null)
                return (List<News>)obj;
            var listNews = News_List(-1, CategoryID, IsActive, SortOrder, null, int.MaxValue - 1, false, false);
            if (listNews != null)
                MpCache.Max(key, listNews);
            return listNews;
        }

        public static List<News> News_HomeOrTypical(int NumberRow, bool IsHome, bool IsTypical)
        {
            return News_List(-1, -1, 0, 0, null, int.MaxValue - 1, IsHome, IsTypical);
        }

        public static List<News> News_Search(string Title, int IsActive)
        {
            return News_List(-1, -1, IsActive, 0, Title, int.MaxValue - 1, false, false);
        }

        public static List<News> News_List(int NewsID, int CategoryID, int IsActive, int SortOrder,
            string Title, int NumberRow, bool IsHome, bool IsTypical)
        {
            return NewsDAO.News_List(NewsID, CategoryID, IsActive, SortOrder, Title, NumberRow, IsHome, IsTypical);
        }

        public static List<News> News_AllPaged(int CategoryID, int IsActive, int SortOrder,
            string Keywords, int PageIndex, int PageSize, out int TotalRow)
        {
            return NewsDAO.News_AllPaged(CategoryID, IsActive, SortOrder, Keywords, PageIndex, PageSize,out TotalRow);
        }
    }
    #endregion

}
